package com.pay.paytoken.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.pay.paytoken.listener.BaseListener;

import appUtils.CustomProgressDialog;
import appUtils.PhoneUtil;
import appUtils.SharedPreferencesManager;
import butterknife.ButterKnife;

public abstract class BaseFragment extends Fragment implements BaseListener {
    protected static SharedPreferencesManager mSharedPreferencesManager;
    protected View view;
    protected Activity mActivity;
    protected Context mContext;
    private View mRootView;
    private CustomProgressDialog progressDialog;
    protected PhoneUtil mPhoneUtil;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        if (mSharedPreferencesManager == null) {
            mSharedPreferencesManager = SharedPreferencesManager.getInstance(getActivity().getApplicationContext());
            mPhoneUtil = new PhoneUtil(getActivity());
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null != mRootView) {
            ViewGroup parent = (ViewGroup) mRootView.getParent();
            if (null != parent) {
                parent.removeView(mRootView);
            }
        } else {
            mRootView = inflater.inflate(setView(), container, false);
        }
        ButterKnife.bind(this, mRootView);
        mActivity = getActivity();
        mContext = getContext();
        initData(savedInstanceState);
        initView();
        return mRootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    protected void showProgress(final int resID) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (progressDialog == null) {
                    progressDialog = new CustomProgressDialog(mContext, getResources().getString(resID));
                }
                progressDialog.show();
            }
        });
    }

    protected void dismisProgress() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (progressDialog != null && progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        mPhoneUtil.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
